package com.example.wanghao.wangyixinwen.menu;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.example.wanghao.wangyixinwen.R;
import com.example.wanghao.wangyixinwen.bean.NewCenterBean;
import com.example.wanghao.wangyixinwen.home.BasePage;
import com.example.wanghao.wangyixinwen.viewpagerindicator.TabPageIndicator;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by wanghao on 2016/5/20.
 */
public class NewPage extends BasePage {
    private NewCenterBean.DataItem mDataItem;//新闻中心传递过来的新闻界面数据


    @Bind(R.id.indicator)
    TabPageIndicator mIndicator;
    @Bind(R.id.pager)
    ViewPager mPager;

    private int currentItem;//记录当前界面的索引



    public NewPage(Context context, NewCenterBean.DataItem dataItem) {
        super(context);
        this.mDataItem = dataItem;
    }

    @Override
    public void onResume() {
        super.onResume();
        //如果是北京,第一个页面的话就设置侧滑菜单全屏模式,否则的话禁止
        if (currentItem == 0){
            mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        }else {
            mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        }
    }


    @Override
    public View initView() {
        /*TextView tv = new TextView(mContext);
        tv.setText("新闻");*/
        //把开源项目作为界面展示
        View view = View.inflate(mContext, R.layout.simple_tabs, null);
        //ViewUtils.inject(this,view);
        ButterKnife.bind(this, view);
        return view;
    }

    //用来新闻界面头部的指针标题
    private List<String> newPageTitles = new ArrayList<>();
   //用来展示新闻中12个频道页面
    private List<BasePage> newPages = new ArrayList<>();

    @Override
    public void initData() {
        newPageTitles.clear();
        newPages.clear();
        for (int i =0;i<mDataItem.children.size();i++){
            newPageTitles.add(mDataItem.children.get(i).title);
            //创建12个频道界面对象newitempage
            newPages.add(new NewItemPage(mContext,mDataItem.children.get(i).url));
        }
        //设置适配器进行展示
        NewAdapter newAdapter = new NewAdapter();
        mPager.setAdapter(newAdapter);

        //指针与viewpager进行绑定
        mIndicator.setViewPager(mPager);

        mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }



            @Override
            public void onPageSelected(int position) {
                currentItem = position;
                //如果是北京,第一个页面的话就设置侧滑菜单全屏模式,否则的话禁止
                if (currentItem == 0){
                    mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                }else {
                    mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
                }

                //当页面被选中的时候,调用对应界面的initdata方法来初始化数据展示界面
                BasePage basePage = newPages.get(position);
                if (!basePage.isLoad){
                    basePage.initData();
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
            //设置北京界面默认显示,设置第一次进入时界面展示
        BasePage basePage = newPages.get(0);
        if (!basePage.isLoad){
            basePage.initData();
        }
    }


    //用来展示viewpager的12个频道的界面
    private class NewAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return newPageTitles.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
           container.addView(newPages.get(position).getRootView());
            return newPages.get(position).getRootView();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }

        //注意:这个方法必须实现,才能将指针界面的标题展示出来
        @Override
        public CharSequence getPageTitle(int position) {
            return newPageTitles.get(position);
        }
    }
}
